package controllers

import javax.inject.{Inject,Singleton}
import play.api.Logging
import play.api.mvc._

import java.nio.file.Paths
import scala.concurrent.ExecutionContext


@Singleton
class LayuiController @Inject()(cc: ControllerComponents)(implicit ec: ExecutionContext, parser: BodyParsers.Default)
  extends AbstractController(cc) with Logging {

  def index() = checkLogin(parser, ec) { implicit request =>
    Ok(views.html.layui.index("layui_index"))

  }


  def uploadForm() = Action { implicit request =>
    Ok(views.html.layui.upload("update"))
  }

  def upload = Action{
    Ok("File uploaded")
  }

  /**
  def upload = Action(parse.multipartFormData) { request =>
    request.body
      .file("excel")
      .map { excelFile =>
        // only get the last part of the filename
        // otherwise someone can send a path like ../../home/foo/bar.txt to write to other files on the system
        val filename = Paths.get(excelFile.filename).getFileName
        val fileSize = excelFile.fileSize
        val contentType = excelFile.contentType
        logger.debug(s"$filename,$fileSize,$contentType")
        //        picture.ref.copyTo(Paths.get(s"./tmp/upload/$filename"), replace = true)
        println(excelFile.ref.getAbsoluteFile.getClass)
        //        picture.ref.asInstanceOf[FileInputStream]
        //ex(s"./tmp/upload/$filename")
        //ex(excelFile.ref.getAbsoluteFile)
        Ok("File uploaded")
      }
      .getOrElse {
        Redirect(".").flashing("error" -> "Missing file")
      }
  }

   *

  def ex(file: java.io.File) = {
    println(file)
    //    val fis = new FileInputStream(file)
    val myWorkbook = new XSSFWorkbook(file)
    val mySheet = myWorkbook.getSheetAt(0)
    val lastNum = mySheet.getLastRowNum
    for (a <- 0 until lastNum) {
      val row = mySheet.getRow(a)
      val metaid = row.getCell(0)
      metaid.setCellType(CellType.STRING)
      val metaidI = metaid.getStringCellValue

      println(metaidI)

    }
    myWorkbook.close()
  }

   */

}

case class LayUiGyErpUser(id: Long, name: String, user: String, account: String, status: String, disable: String)

case class LayUiKdErpUser(id: Long, kddc: String, user: String, name: String, mobile: String, forbid_status: String, disable: String, fAppGroup: String, status: String)

case class LayUiKddc(id: String, name: String, test: String)

